<?php
class Query_workon extends CI_Model {

         public function __construct()
    {
        // Call the Model constructor
        parent::__construct();
        $this->config->load('iss_config');
    }
    
 
     /**
    * execute the query from oracle dbmm
    * @params: the sql statement
    * @return array result + message + state
    */
   public function  select_query($sql_query){
        $state= 1;
        $result = array();
        $message = " ";     
           $connect = oci_connect($this->session->userdata('user_name'), $this->session->userdata('password'), $this->config->item('location').'/'.$this->config->item('database'));
            if (!$connect) {
                 $m = oci_error();
                 $message = $m['message']; 
                 $state =0;
                  $r_return = array (
                        'state' =>$state,
                        'message' => $message,
                        'result' => $result
                    );
                  return $r_return;
            }
           $sql_query = oci_parse($connect, $sql_query);
           
           if (!$sql_query) {
                $m = oci_error($connect);
                 $message = $m['message'];
                 $state =0;
                  $r_return = array (
                        'state' =>$state,
                        'message' => $message,
                        'result' => $result
                    );
                  return $r_return;
          } 
         
           $r = oci_execute($sql_query);
           if (!$r) {
                 $m = oci_error($sql_query);
                $result = $m['message'];
                $message = $m['message'];
                 $state =0;
                  $r_return = array (
                        'state' =>$state,
                        'message' => $message,
                        'result' => $result
                    );
                  return $r_return;
           }
           while (($row = oci_fetch_array($sql_query, OCI_BOTH))) { 
            $result[] = array($row[0],$row[1]);
        }
        
                 $message = 'Execute query success';
                 $state =1;
                 $r_return = array (
                        'state' =>$state,
                        'message' => $message,
                        'result' => $result
                    );
                  return $r_return;
         return $r_return;

    }
    
     public function  update_query($sql_query){
        $state= 1;
        $result = array();
        $message = " ";     
           $connect = oci_connect($this->session->userdata('user_name'), $this->session->userdata('password'), $this->config->item('location').'/'.$this->config->item('database'));
            if (!$connect) {
                 $m = oci_error();
                 $message = $m['message'];
                 $state =0;
                  $r_return = array (
                        'state' =>$state,
                        'message' => $message,
                        'result' => $result
                    );
                  return $r_return;
            }
           $sql_query = oci_parse($connect, $sql_query);
           
           if (!$sql_query) {
                $m = oci_error($connect);
                 $message = $m['message'];
                 $state =0;
                  $r_return = array (
                        'state' =>$state,
                        'message' => $message,
                        'result' => $result
                    );
                  return $r_return;
          } 
         
           $r = oci_execute($sql_query);
           $this->session->set_flashdata('num_row_effected', oci_num_rows($sql_query));
           if (!$r) {
                 $m = oci_error($sql_query);
                $result = $m['message'];
                $message = $m['message'];
                 $state =0;
                  $r_return = array (
                        'state' =>$state,
                        'message' => $message,
                        'result' => $result
                    );
                  return $r_return;
           }
       
                 $message = 'Execute query success';
                 $state =1;
                 $r_return = array (
                        'state' =>$state,
                        'message' => $message,
                        'result' => $result
                    );
                  return $r_return;
         return $r_return;

    }
    
     public function  department_query($sql_query){
        $state= 1;
        $result = array();
        $message = " ";     
           $connect = oci_connect($this->session->userdata('user_name'), $this->session->userdata('password'), $this->config->item('location').'/'.$this->config->item('database'));
            if (!$connect) {
                 $m = oci_error();
                 $message = $m['message'];
                 $state =0;
                  $r_return = array (
                        'state' =>$state,
                        'message' => $message,
                        'result' => $result
                    );
                  return $r_return;
            }
           $sql_query = oci_parse($connect, $sql_query);
           
           if (!$sql_query) {
                $m = oci_error($connect);
                 $message = $m['message'];
                 $state =0;
                  $r_return = array (
                        'state' =>$state,
                        'message' => $message,
                        'result' => $result
                    );
                  return $r_return;
          } 
         
           $r = oci_execute($sql_query);
           if (!$r) {
                 $m = oci_error($sql_query);
                $result = $m['message'];
                $message = $m['message'];
                 $state =0;
                  $r_return = array (
                        'state' =>$state,
                        'message' => $message,
                        'result' => $result
                    );
                  return $r_return;
           }
           while (($row = oci_fetch_array($sql_query, OCI_BOTH))) { 
            $result[] = array($row[0],$row[1]);
        }
        
                 $message = 'Execute query success';
                 $state =1;
                 $r_return = array (
                        'state' =>$state,
                        'message' => $message,
                        'result' => $result
                    );
                  return $r_return;
         return $r_return;

    }


    public function  workon_query($sql_query){
        $state= 1;
        $result = array();
        $message = " ";     
           $connect = oci_connect($this->session->userdata('user_name'), $this->session->userdata('password'), $this->config->item('location').'/'.$this->config->item('database'));
            if (!$connect) {
                 $m = oci_error();
                 $message = $m['message'];
                 $state =0;
                  $r_return = array (
                        'state' =>$state,
                        'message' => $message,
                        'result' => $result
                    );
                  return $r_return;
            }
           $sql_query = oci_parse($connect, $sql_query);
           
           if (!$sql_query) {
                $m = oci_error($connect);
                 $message = $m['message'];
                 $state =0;
                  $r_return = array (
                        'state' =>$state,
                        'message' => $message,
                        'result' => $result
                    );
                  return $r_return;
          } 
         
           $r = oci_execute($sql_query);
           if (!$r) {
                 $m = oci_error($sql_query);
                $result = $m['message'];
                $message = $m['message'];
                 $state =0;
                  $r_return = array (
                        'state' =>$state,
                        'message' => $message,
                        'result' => $result
                    );
                  return $r_return;
           }
           while (($row = oci_fetch_array($sql_query, OCI_BOTH))) { 
            $result[] = array($row[0],$row[1]);
        }
        
                 $message = 'Execute query success';
                 $state =1;
                 $r_return = array (
                        'state' =>$state,
                        'message' => $message,
                        'result' => $result
                    );
                  return $r_return;
         return $r_return;

    }
    


 
    public function  update_workon_query($sql_query){
        $state= 1;
        $result = array();
        $message = " ";     
           $connect = oci_connect($this->session->userdata('user_name'), $this->session->userdata('password'), $this->config->item('location').'/'.$this->config->item('database'));
            if (!$connect) {
                 $m = oci_error();
                 $message = $m['message'];
                 $state =0;
                  $r_return = array (
                        'state' =>$state,
                        'message' => $message,
                        'result' => $result
                    );
                  return $r_return;
            }
           $sql_query = oci_parse($connect, $sql_query);
           
           if (!$sql_query) {
                $m = oci_error($connect);
                 $message = $m['message'];
                 $state =0;
                  $r_return = array (
                        'state' =>$state,
                        'message' => $message,
                        'result' => $result
                    );
                  return $r_return;
          } 
         
           $r = oci_execute($sql_query);
           $this->session->set_flashdata('num_row_effected', oci_num_rows($sql_query));
           if (!$r) {
                 $m = oci_error($sql_query);
                $result = $m['message'];
                $message = $m['message'];
                 $state =0;
                  $r_return = array (
                        'state' =>$state,
                        'message' => $message,
                        'result' => $result
                    );
                  return $r_return;
           }
       
                 $message = 'Execute query success';
                 $state =1;
                 $r_return = array (
                        'state' =>$state,
                        'message' => $message,
                        'result' => $result
                    );
                  return $r_return;
         return $r_return;

    }

  }